#include "car.hpp"
#include "car_struct_csv.hpp"
#include "csv_struct.hpp"
#include "car_power.hpp"
#include "car_power_range.hpp"
#include "car_power_range_ownership.hpp"
#include "fuel_refund.hpp"
#include "travelling.hpp"

#include "file_list.hpp"

car_csv::car_csv(): file(file_list::car_filename())
{}


bool car_csv::exist(int _id)
{	
	return static_cast<car_struct_csv>(find(_id)).id();		
}

bool car_csv::exist(std::string _id)
{	
	return static_cast<car_struct_csv>(find(_id)).id();		
}

float car_csv::total_km(int _plate_id, date& _first_date, date& _last_date, bool _include_last_date)
{
	return travelling_csv::total_km(_plate_id, _first_date, _last_date, _include_last_date);
}

float car_csv::power(int _plate_id)
{	
	float power(static_cast<car_struct_csv>(car_csv::find(_plate_id)).power());
	
	std::cout << "[car_csv::power] power for car "<< _plate_id << " = " << power << std::endl;

	return power;
}

int car_csv::find_power_range_id(int _plate_id)
{
	int power_range[] = { 0, 0 };
	int power_range_id(0);

	car_power_csv::find_range(power(_plate_id), power_range);

	power_range_id = car_power_range_csv::find_power_range_id(power_range);

	std::cout << "[car_csv::find_power_range_id] power range id = " << power_range_id << std::endl;
	
	return power_range_id;
}
	
int car_csv::find_power_range_ownership(int _plate_id)
{
	int power_range_ownership(0);
	int power_range_id(find_power_range_id(_plate_id));
	int ownership_id(ownership(_plate_id));
	
	power_range_ownership = car_power_range_ownership_csv::find_power_range_ownership_id(power_range_id, ownership_id); 
	
	std::cout << "[car_csv::find_power_range_ownership] power range ownership id = " << power_range_ownership << std::endl;
	
	return power_range_ownership;
}

float car_csv::find_fuel_refund_unit(int _power_range_ownership_id, int _fuel_id, date& _travelling_date)
{
	std::cout << "[car_csv::find_fuel_refund_unit] power range ownership id = " << _power_range_ownership_id << "; fuel id = " << _fuel_id << "; date = " << _travelling_date << std::endl;
	
	return fuel_refund_csv::find_fuel_refund_unit(_power_range_ownership_id, _fuel_id, _travelling_date);
}

float car_csv::find_fuel_refund_unit(int _plate_id, date& _travelling_date)
{
	int power_range_ownership_id(find_power_range_ownership(_plate_id));
	int fuel_id(fuel(_plate_id));
		
	return fuel_refund_csv::find_fuel_refund_unit(power_range_ownership_id, fuel_id, _travelling_date);
}


int car_csv::ownership(int _plate_id)
{			
	int ownership_id(static_cast<car_struct_csv>(car_csv::find(_plate_id)).ownership());
	
	std::cout << "[car_csv::ownership] ownership id = " << ownership_id << std::endl;
	
	return ownership_id;
}
	
int car_csv::fuel(int _plate_id)
{	
	int fuel_id(static_cast<car_struct_csv>(car_csv::find(_plate_id)).fuel());
	
	std::cout << "[car_csv::fuel] fuel id = " << fuel_id << std::endl;
	
	return fuel_id;
}


car_struct_csv const car_csv::find(int _id)
{

	csv_struct csv_file(file_list::car_filename());
	while(_id && !csv_file.eof())
	{
		++csv_file;
		car_struct_csv row(csv_file.get_line());
			
		if(row.id() != _id) continue;				
		
		return row;
		
	}		
	
	car_struct_csv car_struct_csv_null;
	return car_struct_csv_null;
}

car_struct_csv const car_csv::find(std::string _id)
{
	csv_struct csv_file(file_list::car_filename());
	
	while(_id.length() && !csv_file.eof())
	{
		++csv_file;
		car_struct_csv row(csv_file.get_line());
			
		if(row.code() != _id) continue;			
		
		return row;					
	}	
	
	car_struct_csv car_struct_csv_null;
	return car_struct_csv_null;
}
